package it.omnisong.server.retrieval;

import it.omnisong.client.model.Link;
import it.omnisong.client.model.Song;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class AbsoluteLyricsDataSource extends AbstractDataSource {

	public AbsoluteLyricsDataSource() {
		this.setBaseUrl("http://www.absolutelyrics.com");
		this.setxPathAlbumQuery("");
		this.setxPathAuthorQuery("id('lyricsinformation')/h2");
		this.setxPathLyricQuery("id('realText')");
		this.setxPathTitleQuery("id('lyricsinformation')/h2");
	}

	@Override
	public List<Link> collectLinksByLetter() {
		List<Link> links 	 = new ArrayList<Link>();
		WebClient webClient  = this.getWebClient();
		String currentArtist = "";
		HtmlPage page 		 = null;

		try {
			page = webClient.getPage(this.getBaseUrl()
					+ "/lyrics/artistlist/a/");
			/* Prende tutti gli artisti */
			List<HtmlElement> elements = (List<HtmlElement>) page.getByXPath(
					"/html//td[3]//li/a"
			);
	
			for (HtmlElement element: elements) {
				currentArtist		= element.asText().replaceAll(" lyrics", "");
				HtmlPage artistPage = element.click();
				/* Prende tutte le canzoni */
				List<HtmlElement> songElements = (List<HtmlElement>) artistPage.getByXPath(
					"//table[@class='e1']//ul/li/a"	
				);
				
				for (HtmlElement songElement: songElements) {
					Song song = new Song();
					Link link = new Link();
					
					song.setTitle(songElement.asText());
					song.setArtist(currentArtist);
					link.setUrl(songElement.getAttribute("href"));
					link.setSong(song);
					links.add(link);
					
					System.out.println("ABSLLRK collecting: " 
							+ song.getTitle() + " - " + song.getArtist() + " - " + link.getUrl());
				}
			}
		} catch (FailingHttpStatusCodeException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return links;
	}

	@Test
	public void testSite() {
		WebClient webClient = this.getWebClient();
		try {
			setCurrentSongUrl("http://www.absolutelyrics.com/lyrics/view/2_hot_4_you/can,27t_take_my_eyes_off_of_you/");
			Song song = getSongMetaData(AS_TEXT);
			
			System.out
					.println(song.getLyric());
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Song getSongMetaData(int retrieveMode)
			throws FailingHttpStatusCodeException, MalformedURLException,
			IOException {

		HtmlPage page = null;
		Song currentSong = new Song();
		page = getWebClient().getPage(getCurrentSongUrl());

		HtmlElement lyric = (HtmlElement) page.getByXPath(getxPathLyricQuery())
				.get(0);
		HtmlElement artist = (HtmlElement) page.getByXPath(
				getxPathAuthorQuery()).get(0);
		HtmlElement title = (HtmlElement) page.getByXPath(getxPathTitleQuery())
				.get(0);

		currentSong.setArtist(artist.asText().split(" - ")[1]);
		currentSong.setTitle(title.asText().split(" -" )[0]);

		if (retrieveMode == AS_TEXT) {
			currentSong.setLyric(lyric.asText());
		} else if (retrieveMode == AS_XML) {
			currentSong.setLyric(lyric.asXml());
		}
		return currentSong;
	}
}
